<?php
class image_fetch extends PHPUnit_Framework_TestCase{

    public function setUp()
    {
		$this->product_model = app::get('tbdata')->model('product');
		$this->image_attach = app::get('image')->model('image_attach');
		$this->image_model = app::get('image')->model('image');
    }
    public function testGetcat(){

		$db = kernel::database();
		
		$rs = $db->exec('select * from sdb_tbdata_product where cid=1512');
		while($row = mysql_fetch_assoc($rs['rs'])){
			$images = array();
			$images[] = $row['pic_url'];
			$base_url = substr($row['pic_url'],0,strpos($row['pic_url'],'uploaded')+9);
			$row['product_imgs'] = unserialize($row['product_imgs']);
			foreach((array)$row['product_imgs']['product_img'] as $img){
				$images[] = $base_url.$img['url'];
			}
			foreach($images as $img){
				$rows = $this->image_model->getList('*',array('ident'=>$img),0,1);
				if($rows[0]){continue;}
				$image_id = $this->image_model->gen_id();
				$this->image_model->store($img,$image_id);
				$data = array();
				$data['target_type'] = 'product';
				$data['image_id'] = $image_id;
				$data['target_id'] = $row['id'];
				$this->image_attach->save($data);
			}
		}

	}
}
